package com.vitaf.cash.app.ui.popupwindow

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.vitaf.cash.app.VitafCashApplication
import com.vitaf.cash.app.R
import com.vitaf.cash.app.base.popup.XGravity
import com.vitaf.cash.app.base.popup.YGravity
import com.vitaf.cash.app.common.VitafComPopup
import com.vitaf.cash.app.databinding.VitafPopupStepGeneralSelectBinding
import com.vitaf.cash.app.ui.adapter.VitafStepSelectPopuApt
import com.vitaf.cash.lib.adapter.VitafOnItemChildClickListener
import org.json.JSONObject

/**
 * GombeCredit
 * @Author wwdeng
 * @CreateTime: 2024/10/22 14:46
 * @Annotation：列表选择
 */
class VitafGeneralPopup(val context: Context) {
    lateinit var binding: VitafPopupStepGeneralSelectBinding
    //开始选择时间
    private var startTime = 0L;

    /* 筛选框宽度，默认屏幕宽度 */
    var popupWidth = VitafCashApplication.app.getPhoneWidth()
    //选中的item
    var selectItem: JSONObject? = null

    private var mItemListener: IItemClickListener? = null

    interface IItemClickListener {
        fun onConfirmAction(data: JSONObject?)
        fun onDismissListener()
    }
    private var mEasyPopup: VitafComPopup? = null

    //分类
    private var itemAdapter: VitafStepSelectPopuApt? = null

    fun show(view: View, dimView: ViewGroup) {
        startTime = System.currentTimeMillis()
        if (mEasyPopup == null) {
            showViewDown(view, dimView)
        } else {
            mEasyPopup?.showAtAnchorView(view, YGravity.BELOW, XGravity.CENTER, 0, 0)
        }
    }

    /* 选择回调 */
    fun setSelectItemListener(listener: IItemClickListener?) {
        mItemListener = listener
    }

    /* 设置宽度 */
    fun setWindowWidth(width: Int){
        this.popupWidth = width
    }

    /* 设置分类数据 */
    fun setBrandList(list: List<JSONObject>) {
        this.itemAdapter?.data = list
    }

    private fun showViewDown(view: View, dimView: ViewGroup) {
        mEasyPopup = VitafComPopup(context)
        binding = VitafPopupStepGeneralSelectBinding.inflate(LayoutInflater.from(context))
        mEasyPopup?.setContentView(binding.root)
        mEasyPopup?.setWidth(this.popupWidth)
//        mEasyPopup?.height = Apps.getPhoneHeight() / 2
        mEasyPopup?.setAnimationStyle(R.style.popup_anim)
        //是否允许点击PopupWindow之外的地方消失
        mEasyPopup?.setFocusAndOutsideEnable(true)
        //允许背景变暗
        mEasyPopup?.setBackgroundDimEnable(false)
        //变暗的透明度(0-1)，0为完全透明
        mEasyPopup?.setDimValue(0.4f)
        //变暗的背景颜色
        mEasyPopup?.setDimColor(Color.BLACK)
        //指定任意 ViewGroup 背景变暗
        mEasyPopup?.setDimView(dimView)
//        mEasyPopup?.apply();
        VitafComPopup.create()
        mEasyPopup?.showAtAnchorView(view, YGravity.BELOW, XGravity.CENTER, 0, 0)
        //排序列表
        initRecyclerView()
        mEasyPopup?.setOnDismissListener { // popupWindow隐藏时恢复屏幕正常透明度
            if (mItemListener != null) {
                mItemListener?.onDismissListener()
            }
        }
    }

    private fun initRecyclerView() {
        binding.selectListView.layoutManager = LinearLayoutManager(context)
        this.itemAdapter = VitafStepSelectPopuApt(binding.selectListView)
        binding.selectListView.adapter = this.itemAdapter
        binding.selectListView.itemAnimator = DefaultItemAnimator()
        this.itemAdapter?.setOnItemChildClickListener(onItemClickListener()) //点击
    }

    /* RecyclerView点击Item */
    internal inner class onItemClickListener: VitafOnItemChildClickListener {
        @SuppressLint("NotifyDataSetChanged")
        override fun onItemChildClick(parent: ViewGroup, childView: View, position: Int) {
            itemAdapter?.let {
                val model = it.getItem(position)
                if (childView.id == R.id.itemLayout) {
                    for (brandItem in it.data) {
                        brandItem.put("isSelect",false)
                        if (brandItem?.optString("title") == model.optString("title")) {
                            brandItem.put("isSelect",true)
                        }
                    }
                    itemAdapter?.notifyDataSetChanged()
                    if (mItemListener != null) {
                        model.put("duration",System.currentTimeMillis() - startTime)
                        mItemListener?.onConfirmAction(model)
                        mEasyPopup?.dismiss()
                    }
                }
            }
        }
    }
}